package com.crape.common.ibatis;

import java.sql.SQLException;
import java.util.List;

import org.springframework.orm.ibatis.SqlMapClientCallback;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.crape.common.exceptions.DAOException;
import com.ibatis.sqlmap.client.SqlMapExecutor;

/**
 * SqlMapClientDaoSupport extends class,<code>catch SQLException</code> <code>throw DAOException</code>
 *
 * @author ChenKai
 * @since 2012-4-22 2:17
 */
public class SqlMapClientDaoSupportEx extends SqlMapClientDaoSupport {

    /**
     * @param statementName
     * @param parameterObject
     * @return
     */
    public Object queryObject(String sqlID, Object obj) {
        try {
            return this.getSqlMapClient().queryForObject(sqlID, obj);
        } catch (SQLException e) {
            throw new DAOException(e);
        }
    }

    /**
     * @param statementName
     * @param parameterObject
     * @return
     */
    public List<?> queryList(String sqlID, Object obj) {
        try {
            return this.getSqlMapClient().queryForList(sqlID, obj);
        } catch (SQLException e) {
            throw new DAOException(e);
        }
    }

    /**
     * @param statementName
     * @param parameterObject
     * @return
     */
    public Object insert(String sqlID, Object obj) {
        try {
            return this.getSqlMapClient().insert(sqlID, obj);
        } catch (SQLException e) {
            throw new DAOException(e);
        }
    }

    /**
     * @param sqlID
     * @param stList
     * @return
     * @throws DAOException
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public Integer insertBatch(final String sqlID, final List<?> stList) throws DAOException {
        Integer result = Integer.valueOf(-1);
        if ((sqlID != null) && (stList != null) && !stList.isEmpty()) {
            result = (Integer) getSqlMapClientTemplate().execute(new SqlMapClientCallback() {

                public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
                    Integer result = Integer.valueOf(-1);
                    executor.startBatch();

                    for (int i = 0; i < stList.size(); i++) {
                    	executor.insert(sqlID, stList.get(i));
                    }
                    result = new Integer(executor.executeBatch());
                    return result;
                }
            });
        }

        return result;
    }

    /**
     * @param statementName
     * @param parameterObject
     * @return
     */
    public int delete(String sqlID, Object obj) {
        try {
            return this.getSqlMapClient().delete(sqlID, obj);
        } catch (SQLException e) {
            throw new DAOException(e);
        }
    }

    /**
     * @param sqlID
     * @param stList
     * @return
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public int deleteBatch(final String sqlID, final List<?> stList) throws DAOException {
        Integer result = Integer.valueOf(-1);
        if ((sqlID != null) && (stList != null) && !stList.isEmpty()) {
            result = (Integer) getSqlMapClientTemplate().execute(new SqlMapClientCallback() {

                public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
                    Integer result = Integer.valueOf(-1);
                    executor.startBatch();

                    for (int i = 0; i < stList.size(); i++) {
                        executor.delete(sqlID, stList.get(i));
                    }
                    result = new Integer(executor.executeBatch());
                    return result;
                }
            });
        }
        return result;
    }

    /**
     * @param statementName
     * @param parameterObject
     * @return
     * @throws DAOException
     */
    public int update(String sqlID, Object obj) throws DAOException {
        try {
            return this.getSqlMapClient().update(sqlID, obj);
        } catch (SQLException e) {
            throw new DAOException(e);
        }
    }

    /**
     * @param sqlID
     * @param stList
     * @return
     * @throws DAOException
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public Integer updateBatch(final String sqlID, final List<?> stList) throws DAOException {
        Integer result = Integer.valueOf(-1);
        if ((sqlID != null) && (stList != null) && !stList.isEmpty()) {
            result = (Integer) getSqlMapClientTemplate().execute(new SqlMapClientCallback() {

                public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
                    Integer result = Integer.valueOf(-1);
                    executor.startBatch();

                    for (int i = 0; i < stList.size(); i++) {
                        executor.update(sqlID, stList.get(i));
                    }
                    result = new Integer(executor.executeBatch());
                    return result;
                }
            });
        }
        return result;
    }
}
